package bktech.soict.hust.bknote.activity.sms;

import java.io.InputStream;

import bktech.soict.hust.bknote.R;

import android.app.Dialog;
import android.content.ContentUris;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactDetail extends Dialog {
	private long id;
	private String displayName;
	private String number;
	private String email;
	
    public ContactDetail(Context context, int theme) {
        super(context, theme);
    }
 
    public ContactDetail(Context context) {
        super(context);
    }
    
    public void setId(long id) {
    	this.id = id;
    	
    	ImageView imageView = (ImageView)findViewById(R.id.contact_image_detail);
    	Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);
        InputStream is = Contacts.openContactPhotoInputStream(getContext().getContentResolver(), contactUri);

        if(is != null)
        {
	        Bitmap bitmap = BitmapFactory.decodeStream(is);
	        imageView.setImageBitmap(bitmap);
        }
        else
        	imageView.setImageBitmap(Bitmap.createBitmap(80, 80, Bitmap.Config.ALPHA_8));        
    }
    
    public long getId() {
    	return id;
    }
 
    public void setDisplayName(String displayName) {
		this.displayName = displayName;
		
		((TextView)findViewById(R.id.display_name)).setText(displayName);
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setNumber(String number) {
		this.number = number;
		
		((TextView)findViewById(R.id.number)).setText(number);
	}

	public String getNumber() {
		return number;
	}

	public void setEmail(String email) {
		this.email = email;
		
		((TextView)findViewById(R.id.email)).setText(email);
	}

	public String getEmail() {
		return email;
	}

	/**
     * Helper class for creating a custom dialog
     */
    public static class Builder {
 
        private Context context;
        private String displayName;
 
        public Builder(Context context) {
            this.context = context;
        }

		public void setDisplayName(String displayName) {
			this.displayName = displayName;
		}

		public String getDisplayName() {
			return displayName;
		}

		public ContactDetail create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            
            // instantiate the dialog with the custom Theme
            final ContactDetail dialog = new ContactDetail(context, 
            		R.style.Dialog);
            
            View layout = inflater.inflate(R.layout.contact_detail, null);
            
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            ((TextView) layout.findViewById(R.id.display_name)).setText(displayName);
            
            dialog.setContentView(layout);
            
            return dialog;
	    }
    }
}